/*
 * AAuthor: Jordan Polaniec
 * Description: A basic pizza ordering app
 * Last Rev:  9.29.11
 * File: PizzaOrderAppActivity.java
 */

package com.polaniec.pizzaorderapp;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class PizzaOrderAppActivity extends Activity {
    /** Called when the activity is first created. */
	
	//connect elements to prog
	EditText name, phone;
	RadioGroup type1, type2;
	CheckBox pep, mush, veg, anch, olive, pine;
	Button submitBtn;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //instantiate elements connected to vars
        name = (EditText)findViewById(R.id.ETName);
        phone = (EditText)findViewById(R.id.ETphone);
        type1 = (RadioGroup)findViewById(R.id.radioGroupType1);
        type2 = (RadioGroup)findViewById(R.id.radioGroupType2);
        pep = (CheckBox)findViewById(R.id.checkBoxPep);
        mush = (CheckBox)findViewById(R.id.checkBoxMush);
        veg = (CheckBox)findViewById(R.id.checkBoxMush);
        anch = (CheckBox)findViewById(R.id.checkBoxAnchovies);
        olive = (CheckBox)findViewById(R.id.checkBoxOlives);
        pine = (CheckBox)findViewById(R.id.checkBoxPineapple);
        submitBtn = (Button)findViewById(R.id.submitBtn);
        
        
        //init values for radiobuttons
        type1.check(-1); //-1 is unchecked
        type2.check(-1);
        
        //setup menu creation
        
        
        //setup listeners//
        
        //name editext
        name.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				name.setText("");
				name.setTextColor(Color.BLACK);			
			}
		});
        
        //phone editext
        phone.setOnClickListener(new OnClickListener(){
        	@Override
        	public void onClick(View v)
        	{
        		phone.setText("");
        		phone.setTextColor(Color.BLACK);
        	}
        });
        
        //type1
        type1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				// get the radiobutton selected
				// get it's text
				// if there is current text, erase it in order
				// add current text to order
				// implement later after learning geolocation and phone intents
				
			}
		});
        
        //type2
        type2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
			
				// get the radiobutton selected
				// get it's text
				// if there is current text, erase it in order
				// add current text to order
				// implement later after learning geolocation and phone intents
				
			}
		});
        
       submitBtn.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			//validate the order
			orderValidation();
		}
	});

    }
    
    //validate the current order before sending
    public boolean orderValidation()
    {
    	//make sure the correct fields have been filled in/ checked
		if((name.getText().length() > 0 && phone.getText().length() > 0) && 
				(type1.getCheckedRadioButtonId() != -1 && type2.getCheckedRadioButtonId() != -1)&&
					pep.isChecked() || mush.isChecked() || veg.isChecked() || 
					anch.isChecked() || olive.isChecked() || pine.isChecked())
		{
			
					//show a message saying the order has been sent
					Toast.makeText(getApplicationContext(), "Order Submitted!", Toast.LENGTH_SHORT).show();
					/*
					 * LATER: SEND A TEXT TO SOMEONE TO RECEIVE THE ORDER AS A TEXT??
					 */
					return true;
		}
			
		
		else //if not..
		{
			Toast.makeText(getApplicationContext(), "Please be sure to fill out all fields.", Toast.LENGTH_SHORT).show();
			return false;
		}
    	    	

    }
    
    //handles the creation and inflation of the menu - by pressing the menu button on device
 	 @Override
 	 public boolean onCreateOptionsMenu(Menu menu)
 	 {
 		 MenuInflater inflater = getMenuInflater();
 		 inflater.inflate(R.menu.menu, menu);
 		 return true;
 	 }
 	 
 	 //handles menu item actions
 	 @Override
 	 public boolean onOptionsItemSelected(MenuItem item)
 	 {
 		 //handle item selection
 		 if(item.getItemId() == R.id.clearOrder)
 		 {
 			//clear all of the current selections
 			name.setTextColor(R.color.init_text);
 			name.setText("Enter Your Name");
 			phone.setTextColor(R.color.init_text);
 			phone.setText("Enter Your Phone Number");
 			
 			type1.clearCheck();
 			type2.clearCheck();
 			
 			pep.setChecked(false);
 			mush.setChecked(false);
 			veg.setChecked(false);
 			anch.setChecked(false);
 			olive.setChecked(false);
 			pine.setChecked(false);
 			 
	  	    return true;
 		 }
 		 return false;
 	 }
}